package com.example.demo.buessiness.company.controller;

import com.example.demo.buessiness.company.interfaces.ICompanyService;
import com.example.demo.buessiness.company.model.dto.CompanyRequest;
import com.example.demo.buessiness.company.model.dto.CompanyResponse;
import com.example.demo.buessiness.user.model.dto.UserRequest;
import com.example.demo.buessiness.user.model.dto.UserResponse;
import msa.common.model.ResultCode;
import msa.common.model.ResultModel;
import msa.common.utils.ResultModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value ="/fb")
@CrossOrigin(allowCredentials = "true", allowedHeaders = {"Authentication-Info", "content-type"},
        exposedHeaders = {"Authentication-Info"})
public class CompanyController {
    @Autowired
    ICompanyService companyService;

    @GetMapping(value = "/findcompany/{id}")
    public ResultModel findCompanyById(@PathVariable("id") Long id){
        try{
            return companyService.findCompanyById(id);
        }catch(Exception e){
            return ResultModelUtils.getFailInstance(ResultCode.BUSINESS_ERROR.toString(),e.getMessage());
        }
    }

    //添加
    @PostMapping(value = "/addCompany")
    public ResultModel addCompany(@RequestBody CompanyRequest comRequest){
        return companyService.addCompany(comRequest);
        //return null;
    }
    //更新
    @PutMapping(value = "/updateCompany",produces = "application/json;charset=UTF-8")
    public ResultModel updateCompany(@RequestBody CompanyRequest comRequest){
        return companyService.updateCompany(comRequest);
    }

    //删除
    @DeleteMapping(value = "/deleteCompany")
    public ResultModel deleteCompany(@RequestParam("id") Long id){
        try{
            return companyService.deleteCompany(id);
        }catch (Exception e){
            return ResultModelUtils.getFailInstance();
        }
    }
    //查询所有
    @GetMapping(value = "/companyAll")
    public ResultModel findAll(){
        try{
            return ResultModelUtils.getSuccessInstance((companyService.findAll()));
        }catch (Exception e){
            return ResultModelUtils.getFailInstance (ResultCode.BUSINESS_ERROR.toString (),e.getMessage ());
        }
    }

    @PutMapping(value = "/modifyById")
    public ResultModel modify(@RequestBody CompanyRequest comRequest){
          System.out.println(comRequest.getAddress());
          System.out.println(comRequest.getId());
          companyService.modifyCompanyByName(comRequest.getAddress(),comRequest.getId());
          return ResultModelUtils.getFailInstance();
    }

    //查询公司名
    @PostMapping("/findByCompany")
    public ResultModel findByCompany(@RequestBody CompanyRequest companyRequest) {

        try {
            List<CompanyResponse> companyResponses = companyService.findByCompany(companyRequest.getCompany());
            if (companyResponses.isEmpty()) {
                return ResultModelUtils.getInstance(false);
            } else {
                return ResultModelUtils.getSuccessInstance(companyResponses);
            }
        } catch (Exception e) {
            return ResultModelUtils.getFailInstance(ResultCode.BUSINESS_ERROR.toString(), e.getMessage());
        }
    }





}
